package datasource

import (
	"fmt"
	"gitee.com/go-caipu-team/go-caipu-plugin-sdk/backend"
	"gitee.com/go-caipu-team/go-caipu-plugin-sdk/backend/tracing"
	"gitee.com/go-caipu-team/go-caipu-plugin-sdk/internal/automanagement"
)

type ManageOpts struct {
	GRPCSettings backend.GRPCSettings

	// TracingOpts contains settings for tracing setup.
	TracingOPts tracing.Opts
}

func Manage(pluginID string, instanceFactory InstanceFactoryFunc, opts ManageOpts) error {
	backend.SetupPluginEnvironment(pluginID)
	if err := backend.SetupTracer(pluginID, opts.TracingOPts); err != nil {
		return fmt.Errorf("setup tracer:%w", err)
	}
	handler := automanagement.NewManager(NewInstanceManager(instanceFactory))
	return backend.Manage(pluginID, backend.ServeOpts{
		CheckHealthHandler:  handler,
		CallResourceHandler: handler,
		QueryDataHandler:    handler,
		StreamHandler:       handler,
		GRPCSettings:        opts.GRPCSettings,
	})
}
